package com.module.wx.controller;

import java.io.File;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.file.utils.FileUploadUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.web.multipart.MultipartFile;
import com.module.wx.domain.ZzdOrderInfo;
import com.module.wx.service.IZzdOrderInfoService;
import com.module.wx.util.Util;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-01-09
 */
@RestController
@RequestMapping("/orderInfo")
public class ZzdOrderInfoController extends BaseController
{
    @Autowired
    private IZzdOrderInfoService zzdOrderInfoService;

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("wx-applet:orderInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(ZzdOrderInfo zzdOrderInfo)
    {
        startPage();
        List<ZzdOrderInfo> list = null;
        //获取当前登录用户信息
        LoginUser login_user = SecurityUtils.getLoginUser();
        //如果是管理员登录,则查询全部数据，否则只能查询当前商家的
        if((login_user.getSysUser().getUserName()).equals("admin")){
            list = zzdOrderInfoService.selectZzdOrderInfoList(zzdOrderInfo);
        }else{
            list = zzdOrderInfoService.selectZzdOrderInfoListByStoreId(login_user.getSysUser().getUserId().toString());
        }
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("wx-applet:orderInfo:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZzdOrderInfo zzdOrderInfo)
    {
        List<ZzdOrderInfo> list = zzdOrderInfoService.selectZzdOrderInfoList(zzdOrderInfo);
        ExcelUtil<ZzdOrderInfo> util = new ExcelUtil<ZzdOrderInfo>(ZzdOrderInfo.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @RequiresPermissions("wx-applet:orderInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(zzdOrderInfoService.selectZzdOrderInfoById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @RequiresPermissions("wx-applet:orderInfo:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZzdOrderInfo zzdOrderInfo)
    {
        zzdOrderInfo.setId(Util.get_radom_id());
        zzdOrderInfo.setCretTime(new Date());
        return toAjax(zzdOrderInfoService.insertZzdOrderInfo(zzdOrderInfo));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("wx-applet:orderInfo:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZzdOrderInfo zzdOrderInfo)
    {
        zzdOrderInfo.setUpdtTime(new Date());
        return toAjax(zzdOrderInfoService.updateZzdOrderInfo(zzdOrderInfo));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("wx-applet:orderInfo:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(zzdOrderInfoService.deleteZzdOrderInfoByIds(ids));
    }

    /**
     *  上传图片到服务器
     */
    @RequestMapping(value = "/upload/img", method = RequestMethod.POST)
    public AjaxResult upload_image(HttpServletRequest request, HttpServletRequest response) throws IOException {
        String file_path = "D:\\ruoyi\\uploadPath";
        String file_show_path = "http://127.0.0.1:9300/statics";
        AjaxResult ajax = AjaxResult.success();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
        //savePath =  this.uploadLocal(file,bizPath);  //调用上传方法返回保存的路径
        String avatar = FileUploadUtils.upload(file_path, file);
        ajax.put("imgUrl", file_show_path+avatar);
        return ajax;
    }

    /**
     * 查询所有商家名称
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/getStoreName")
    public AjaxResult getStoreName() throws SQLException, ClassNotFoundException {
        return success(Util.getMysqlConnectionResult("select user_id as id,nick_name as nickName from sys_user where del_flag <> '2'"));
    }

    /**
     *
     * @return 获取各食堂统计图表信息
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @RequiresPermissions("system:user:list")
    @GetMapping(value = "/getgstdd/{ret}")
    public AjaxResult getgstdd(@PathVariable("ret") String ret) throws SQLException, ClassNotFoundException {
        // 获取前端参数（学校站点id、机构id，开始时间，结束时间），如果是用户点击查询
        if(ret!=null&&!ret.equals("")){


        }
        return success("");
    }

    /**
     * 获取所有学校站点信息
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @GetMapping("/getSchool")
    public AjaxResult getSchool() throws SQLException, ClassNotFoundException {
        return success(Util.getOrgListData("select t1.dept_id as id,t1.dept_name as orgName from sys_dept t1,(SELECT parent_id,dept_id,length(ancestors) - length(replace(ancestors,',',''))+1 as count   FROM sys_dept) t2 where t1.dept_id=t2.dept_id and t1.del_flag !=2 and t2.count=2"));
    }

    /**
     * 获取所有机构信息
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/getOrg/{schoolId}")
    public AjaxResult getOrg(@PathVariable("schoolId") String schoolId) throws SQLException, ClassNotFoundException {
        return success(Util.getOrgListData("select t1.dept_id as id,t1.dept_name as orgName from sys_dept t1,(SELECT parent_id,dept_id,length(ancestors) - length(replace(ancestors,',',''))+1 as count   FROM sys_dept) t2 where t1.dept_id=t2.dept_id and t1.del_flag !=2 and t2.count=3 and t2.parent_id="+schoolId));
    }

    /**
     * 根据商家id获取商家对应菜品信息
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @GetMapping("/getOrderByStoreId/{storeId}")
    public AjaxResult getOrderByStoreId(@PathVariable("storeId") String storeId){
        List<ZzdOrderInfo> list = null;
        if(storeId!=null&&!storeId.equals("")){
            list = zzdOrderInfoService.selectOrderByStoreId(storeId);
        }
        return success(list);
    }
}
